<?php
set_time_limit(0);
#echo dirname(__FILE__).'/simple_html_dom.php';exit;
include_once dirname(__FILE__).'/simple_html_dom.php';
class Zq {
	public function baidu($row){
		
		$url="http://dict.baidu.com/s?wd=".$row->word."";
		//echo $url;
		$html= file_get_html($url);
		//echo $html;exit;
		if(!$html->find("#pronounce"))return;
		$base =$html->find("#pronounce",0);
		
		
		if($base->find("span")){
			//echo 'addd';exit;
			foreach($base->find("span") as $r){
				if (strstr($r->innertext,'英')){
					$s=$r->find('b',0)->innertext;
					$p=$r->find('a',0)->url;
					$this->saveP($s,$p,$row->id,1,1);
						
				}elseif(strstr($r->innertext,'美')){
					$s=$r->find('b',0)->innertext;
					$p=$r->find('a',0)->url;
					$this->saveP($s,$p,$row->id,2,1);
				}else{
					$s=$r->find('b',0)->innertext;
					$p=$r->find('a',0)->url;
					$this->saveP($s,$p,$row->id,0,1);
				}
			}
		}elseif ($base->find("a")){
			//echo 'nb';exit;
			$p=$base->find('a',0)->url;
			$this->saveP("",$p,$row->id,0,1);
		}
	}
	public function saveP($s,$p,$id,$t,$r){
		
		$pr = new Pronunciation();
		if($row=$pr->find("type=:type AND reference=:r AND word_id=:id",array(':type'=>$t,':r'=>$r,':id'=>$id))){
			
			$row->symbol=$s;
			$row->pronunciation=$p;
			$row->save();
			//print_r($row);exit;
		}else{
		$pr->symbol=$s;
		$pr->pronunciation=$p;
		$pr->type=$t;
		$pr->reference=$r;
		$pr->word_id=$id;
		$pr->save();
		}
	}
	public function youdao($row){
		$w=$row->word;
		$url="http://dict.youdao.com/search?le=eng&q=".$w."&keyfrom=dict.index";
		//echo $url;
		$html= file_get_html($url);
		$englishE="";
		$englishA="";
		$english0="";
		$info="";
		$ee="";
		$dict21="";
		$kl="";
		if($html->find(".error-wrapper")){
			return null;
		}
		$row->save();
		foreach ($html->find(".pronounce") as $r){
		
			if (strstr($r->innertext,'英')){
				if($r->find('.phonetic',0))
				$englishE=$r->find('.phonetic',0)->innertext;
			}elseif(strstr($r->innertext,'美')){
				if($r->find('.phonetic',0)){
				$englishA=$r->find('.phonetic',0)->innertext;
				}
			}else{
				if($r->find('.phonetic',0))
				$english0=$r->find('.phonetic',0)->innertext;
			}
		};
		if($html->find(".trans-container",0))
		$info = $html->find(".trans-container",0)->innertext;
		else
		{echo $w;exit;}
		if($html->find("#tEETrans",0))
		$ee = $html->find("#tEETrans",0)->innertext;
		if($html->find("#authDictTrans",0))
		$dict21 = $html->find("#authDictTrans",0)->innertext;
		if($html->find("#collinsResult",0))
		$kl = $html->find("#collinsResult",0)->innertext;
		$data= array('e'=>$englishE,'a'=>$englishA,'o'=>$english0,'i'=>$info,'ee'=>$ee,'d21'=>$dict21,'kl'=>$kl);
		if(!empty($data['e'])){
			$this->saveP($data['e'], '', $row->id, 1,0);
		}
		if(!empty($data['a'])){
			$this->saveP($data['a'], '', $row->id, 2,0);
		}
		if(!empty($data['o'])){
			$this->saveP($data['o'], '', $row->id, 0,0);
		}
		
		if(!empty($data['ee'])){
			$this->saveD($row['id'], 1, $data['ee']);
		}
		if(!empty($data['d21'])){
			$this->saveD($row['id'], 2, $data['d21']);
		}
		if(!empty($data['kl'])){
			$this->saveD($row['id'], 3, $data['kl']);
		}
		if(!empty($data['i'])){
			$this->saveD($row['id'], 0, $data['i']);
		}
		return $row;
	}
	public function saveD($id,$type,$ex){
		$d = new Dict();
		if($r = $d->find("type=:t AND word_id=:id",array(':t'=>$type,':id'=>$id))){
			$r->explanation=$ex;
			$r->save();
		}else{
			$d->word_id=$id;
			$d->type=$type;
			$d->explanation=$ex;
			$d->save();
		}
	}
	public function liju($word){
		$w=$word->word;
		$url="http://dict.youdao.com/search?le=eng&q=lj%3A".$w."&keyfrom=dict.index";
		$data=array();
		$html= file_get_html($url);
		$base =$html->find("#bilingual",0);
		if($base->children(0)->tag=="ul"){
			$bbase=$base->children(0);
		}elseif($base->children(1)->tag=="ul"){
			$bbase=$base->children(1);
		}else{
			echo("!!!").$w;
			return null;
		}
		foreach ($bbase->children() as $r){
			$data[]=array('e'=> strip_tags($r->children(0)->innertext),
				'c'=> strip_tags($r->children(1)->innertext),
				'r'=> strip_tags($r->children(2)->innertext)
			);
		};
		foreach ($data as $lj){
			$this->saveL($lj['e'],$lj['c'],$lj['r'],$word->id);		
		}
	}
	public function saveL($english,$chinese,$reference,$id){
		$e = new Example();
		if($r = $e->find("english=:e",array(':e'=>$english))){
			$r->words.=$id.",";
			$r->save();
		}else{
		$e->english=$english;
		$e->chinese=$chinese;
		$e->reference=$reference;
		$e->words=$id.",";
		$e->save();
		}
	}
}
